import router from '@/router'

interface routerData {
    parentId: number,
    menuId: number,
    name: string,
    code: string,
    icon?: string,
    path: string,
    children?: Array<any>,
}

export const formatAddRoute = (routes: Array<any>) => {
    console.log(7888, routes)
    routes.map((itemRouter: routerData) => {
        if (!router.hasRoute(itemRouter.name)) {
            router.addRoute('Index', {
                path: itemRouter.path,
                name: itemRouter.code,
                component: () => import(`@/views${itemRouter.path}.vue`)
            })

            if (itemRouter.children && itemRouter.children.length > 0) {
                formatAddRoute(itemRouter.children)
            }
        }
    })
    console.log(7999, router.getRoutes())
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    if (!router.hasRoute(Error)) {
        router.addRoute('Index', {
            path: '/:pathMatch(.*)',
            name: 'Error',
            component: () => import('@/views/error/index.vue'),
            meta: {title: '404'}
        })
    }
    return routes
}

export const getCurrentMenuByMenuId = (menuId: number, menuList: Array<any>) => {
    let item = {}
    const getData = (menuId: number, menuList: Array<any>) => {
        for (let i = 0; i < menuList.length; i++) {
            if (menuList[i].menuId === menuId) {
                item = menuList[i]
                return
            } else {
                if (menuList[i].children && menuList[i].children.length > 0) {
                    getData(menuId, menuList[i].children)
                }
            }
        }
    }
    getData(menuId, menuList)
    return item
}
